libDB
	parent_type = /libBase
	name = "libDB"
	numVerP = 0
	numVerM = 0
	numVerR = 0

	var
		DBConnection/DBCon
		DB_SERVER	= "server"
		DB_PORT		= 1234
		DB_NAME		= "database"
		DB_USER		= "user"
		DB_PWORD	= "pword"

		mysql/mysql = new()

	proc
		setServer(server as text)
			DB_SERVER = server
			world.log<<"SERVER = [getServer()]"
		setPort(port as num)
			DB_PORT = port
			world.log<<"PORT = [getPort()]"
		setDB(db as text)
			DB_NAME = db
			world.log<<"DB = [getDB()]"
		setUser(user as text)
			DB_USER = user
			world.log<<"USER = [getUser()]"
		setPWord(pword as text)
			DB_PWORD = pword
			world.log<<"PASS = [getPass()]"

		getDB()			return DB_NAME
		getServer()		return DB_SERVER
		getPort()		return DB_PORT
		getUser()		return DB_USER
		getPass()		return DB_PWORD

		createDBI()		return "dbi:mysql:[getDB()]:[getServer()]:[getPort()]"

		establishConnection()
			world.log<<"Connecting..."
			if(!DBCon)
				world.log<<"Creating Connection..."
				DBCon = new(createDBI(), getUser(), getPass())
				if(!DBCon.IsConnected()) world.log<<DBCon.ErrorMsg()
			else if(!(DBCon.IsConnected()))
				world.log<<"Re-establishing Connection ..."
				DBCon.Connect(createDBI(), getUser(), getPass())
				if(!DBCon.IsConnected()) world.log<<DBCon.ErrorMsg()
			world.log<<"Connected! [DBCon] ([DBCon.IsConnected()])"
			return DBCon

		forceNewConnection()
			world.log<<"!!!FORCING NEW CONNECTION!!!"
			world.log<<"DBI = [createDBI()]"
			DBCon = new(createDBI(), getUser(), getPass())
			if(!DBCon.IsConnected())
				world.log<<DBCon.ErrorMsg()

		Initialize(server, port, db, user, pass)
			setServer(server)
			setDB(db)
			setPort(port)
			setUser(user)
			setPWord(pass)

		Query(sql)
			var/DBQuery/query = DBCon.NewQuery(sql)
			return query

		runQuery(sql as text)
			var/DBQuery/query = DBCon.NewQuery(sql)
			if(!query.Execute()) Core.Error.catchError("type=DB;source=libDB:libDB.runQuery();error=Failed Query ([query.ErrorMsg()]);debug='SQL=[sql]")
			return query

		queryData(sql as text, target=world.log)
			var/DBQuery/query = runQuery(sql)
			if(query)
				var/header = FALSE
				while(query.NextRow())
					var/list/row = query.GetRowData()
					if(!header)
						for(var/col in row) target<<"|[col]\t\..."
						world.log<<"\n"

					for(var/col in row) target<<"|[row[col]]\t\..."
					world.log<<"\n"


		insertData(insertstring)
			world.log<<"insertData([insertstring])"
			var/list/parse = params2list(insertstring)
			var/tblname = parse["tbl"]
			var/list/cols = list()
			var/list/data = list()
			if(parse.len > 1)
				for(var/x = 2, x<= parse.len, x++)
					var/col = parse[x]
					var/dat = parse[col]
					cols.Add(col)
					data.Add(dat)

				var/sql = "INSERT INTO `[tblname]` ("
				for(var/c in cols)
					sql += "`[c]`,"
				sql = copytext(sql, 1, lentext(sql))
				sql += ") VALUES ("
				for(var/d in data)
					if(length(d) == 0)
						d = "NULL"
					sql += "[d],"
				sql = copytext(sql, 1, lentext(sql))
				sql += ");"

				src.runQuery(sql)



		createTable(tblstring)
			var/list/parse = params2list(tblstring)
			var/tblname = parse["name"]
			var/fielddata = parse["fields"]
			world.log<<"FDATA = [fielddata]"
			var/keyinf = parse["keys"]
			world.log<<"KINFO = [keyinf]"
			var/engine = parse["eng"]
			var/fstring = ""
			var/list/fields = dd_text2list(fielddata,"|")
			for(var/field in fields) fstring += "[field],"
			fstring = copytext(fstring,1,lentext(fstring))
			var/keystring = ""
			if(!(isnull(keyinf)))
				keystring = "PRIMARY KEY ("
				var/list/keys = dd_text2list(keyinf, "|")
				for(var/key in keys) keystring+="`[key]`,"
				keystring = copytext(keystring,1,lentext(keystring))
				keystring += ")"

			var/string = ""
			if(lentext(fstring)>0)	string += fstring
			if(lentext(keystring)>0) string += ", [keystring]"

			var/sql = "CREATE TABLE `[getDB()]`.`[tblname]` ([string])"
			if(!(isnull(engine))) sql += " ENGINE=[engine];"
			else sql += " ENGINE=InnoDB;"

			src.queryData(sql)

			/*name=tblTest;fields=col1 varchar(255)|col2 integer unsigned|col3 integer unsigned not null;keys=col3;eng=InnoDB

			name=tblTest
			fields
				col1 varchar(255)
				col2 integer unsigned
				col3 integer unsigned not null*/




/*
insertData(tbl=core_Error;type="DB";source="libDB:libDB.runQuery()";location="";
errorInf="Failed Query (Incorrect integer value: '[0x3000000]' for column 'mobID' at row 1)";
debugInf="'SQL=INSERT INTO `core_CurrentMobs` (`mobID`,`name`,`location`,`client`) VALUES ("[0x3000000]","mob","","")";errorDate="2009-02-27 12:14:40")



insertData(tbl=core_Error;type="DB";source="libDB:libDB.runQuery()";location="";
errorInf="Failed Query (You have an error in your SQL syntax";
debugInf="'SQL=INSERT INTO `core_Error` (`type`,`source`,`location`,`errorInf`,`debugInf`,`errorDate`) VALUES ("DB","libDB:libDB.runQuery()","","Failed Query (Incorrect integer value: '[0x3000000]' for column 'mobID' at row 1)","'SQL=INSERT INTO `core_CurrentMobs` (`mobID`,`name`,`location`,`client`) VALUES ("[0x3000000]","mob","","")","2009-02-27 12:14:40")";errorDate="2009-02-27 12:14:40")
*/


DBQuery
	proc
		SQL()
			return src.sql